<template>
  <div class="login-container">
    <div class="login-weaper">
      <div class="login-left">
        <div class="login-time">{{ loginTime | updatetime }}</div>
        <img src="../../assets/logo.png" alt="" class="img" />
        <p class="title">发票管理平台</p>
      </div>
      <div class="login-border">
        <div class="login-main">
          <h4 class="login-title">登录</h4>
          <a-form-model ref="ruleForm" :model="form2" :rules="rules">
            <!-- <a-form-model-item prop="database" label="数据库">
              <a-select v-model="form2.database">
                <a-select-option
                  v-for="item in database_options"
                  :value="item"
                  :key="item"
                >
                  {{ item }}
                </a-select-option>
              </a-select>
            </a-form-model-item> -->

            <a-form-model-item prop="username">
              <a-input v-model="form2.username" placeholder="用户账号">
                <a-icon slot="prefix" type="user" />
              </a-input>
            </a-form-model-item>

            <a-form-model-item prop="password">
              <a-input-password v-model="form2.password" placeholder="密码">
                <a-icon slot="prefix" type="lock" />
              </a-input-password>
            </a-form-model-item>

            <a-form-model-item prop="verificationCode">
              <a-input v-model="form2.verificationCode" placeholder="验证码">
                <a-icon slot="prefix" type="safety-certificate" />
                <div @click="getCodeNum" slot="suffix" class="login-code-img">
                  {{ codeNum }}
                </div>
              </a-input>
            </a-form-model-item>

            <a-form-model-item>
              <a-button type="primary" @click="onSubmit" block :size="size">
                登录
              </a-button>
            </a-form-model-item>
          </a-form-model>
        </div>
      </div>
    </div>

    <div v-if="false"></div>
  </div>
</template>

<script>
import loginMixin from './loginMixin'
import moment from 'moment'
import 'moment/locale/zh-cn' // 获取中国标准时间，避免出现utc时间
export default {
  name: 'AppLogin',
  components: {},
  mixins: [loginMixin],

  data() {
    return {
      loginTime: new Date(),
      size: 'large'
    }
  },
  filters: {
    updatetime: function (value) {
      return moment(value).format('YYYY-MM-DD HH:mm:ss')
    }
  },
  computed: {},
  async created() {
    this.init()
  },

  methods: {
    onSubmit() {
      this.$refs.ruleForm.validateField('verificationCode', err => {
        if (err === '') {
          this.$refs.ruleForm.validate(valid => {
            if (valid) {
              const success = res => {
                console.log('login ok:', res)
                // Toast.success(res.name + '登录')
                // debugger;
                this.$router.push({ path: '/' })
                this.getCodeNum()
              }

              // eslint-disable-next-line no-unused-vars
              const error = e => {
                // console.log(e)
                this.getCodeNum()
              }
              // 深拷贝 删除参数verificationCode
              let formParams = JSON.parse(JSON.stringify(this.form2))
              delete formParams.verificationCode
              this.handleLogin(formParams, success, error)
            } else {
              console.log('error submit!!')
              return false
            }
          })
        }
      })
    }
  },
  mounted() {
    //生命周期模板挂载之后
    let _this = this
    this.timer = setInterval(() => {
      _this.loginTime = new Date()
    }, 1000)
    this.getCodeNum()
  },
  beforeDestroy() {
    //生命周期实例销毁之前
    if (this.timer) {
      clearInterval(this.timer)
    }
  }
}
</script>

<style type="text/css">
@keyframes animate-cloud {
  0% {
    background-position: 600px 100%;
  }
  100% {
    background-position: 0 100%;
  }
}
@media (max-width: 999px) {
  .login-weaper {
    width: 96% !important;
  }
  .login-left {
    display: none !important;
  }
  .login-border {
    border-radius: 5px;
    padding: 40px;
    margin: 0 auto;
    float: none !important;
    width: 100% !important;
  }
}
@media (min-width: 999px) {
  .login-weaper {
    width: 1000px;
  }
}
.login-container {
  display: flex;
  align-items: center;
  position: relative;
  width: 100%;
  height: 100%;
  margin: 0 auto;
  background: url(http://www.17sucai.com/preview/242158/2015-01-10/%E7%99%BB%E5%BD%95/images/cloud.jpg)
    0 bottom repeat-x #049ec4;
  -webkit-animation: animate-cloud 20s linear infinite;
  animation: animate-cloud 20s linear infinite;
}
.login-weaper {
  margin: 0 auto;
  -webkit-box-shadow: -4px 5px 10px rgb(0 0 0 / 40%);
  box-shadow: -4px 5px 10px rgb(0 0 0 / 40%);
}
.login-left {
  border-top-left-radius: 5px;
  border-bottom-left-radius: 5px;
  justify-content: center;
  flex-direction: column;
  background-color: #409eff;
  color: #fff;
  float: left;
  width: 50%;
  position: relative;
}
.login-left > .img {
  width: 140px;
}
.login-left .title {
  margin-top: 60px;
  text-align: center;
  color: #fff;
  font-weight: 300;
  letter-spacing: 2px;
  font-size: 25px;
}
.login-border {
  border-left: none;
  border-top-right-radius: 5px;
  border-bottom-right-radius: 5px;
  color: #fff;
  background-color: #fff;
  width: 50%;
  float: left;
  box-sizing: border-box;
}
.login-left,
.login-border {
  position: relative;
  min-height: 500px;
  align-items: center;
  display: flex;
}
.login-main {
  margin: 0 auto;
  width: 65%;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
}
.login-title {
  color: #333;
  margin-bottom: 40px;
  font-weight: 500;
  font-size: 22px;
  text-align: center;
  letter-spacing: 4px;
}
.login-time {
  position: absolute;
  left: 25px;
  top: 25px;
  width: 100%;
  color: #fff;
  font-weight: 200;
  opacity: 0.9;
  font-size: 18px;
  overflow: hidden;
}
.login-code-img {
  margin-top: 2px;
  width: 100px;
  height: 36px;
  background-color: #fdfdfd;
  border: 1px solid #f0f0f0;
  color: #333;
  font-size: 14px;
  font-weight: bold;
  letter-spacing: 5px;
  line-height: 34px !important;
  text-indent: 5px;
  text-align: center;
  right: -13px;
  top: -26px;
  position: absolute;
}
.ant-input:focus {
  /* 去除获取焦点时的样式 */
  border: none;
  box-shadow: none !important;
  border-bottom: 1px solid #dcdfe6;
  /* border-bottom: ; */
}
.ant-input {
  /* 去除边框样式 */
  resize: none;
  border: none;
  height: 30px;
  border-bottom: 1px solid #dcdfe6 !important;
  border-radius: 0 !important;
}
.has-error .ant-input-affix-wrapper .ant-input:focus {
  border: none;
  box-shadow: none !important;
  border-bottom: 1px solid red !important;
}
.has-error .ant-input-affix-wrapper .ant-input {
  border-bottom: 1px solid red !important;
}
.ant-btn-primary {
  background: rgba(0, 0, 0, 0) !important;
  color: #1890ff !important;
}
.ant-btn-primary:focus {
  background: #1890ff !important;
  color: white !important;
}
</style>
